<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.keyword" placeholder="关键字"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="search" round>查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd" round>新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="dictionaryTypes" highlight-current-row v-loading="listLoading" @row-dblclick="openClick"
              @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="name" label="名称" width="240" sortable>
			</el-table-column>
			<el-table-column prop="sn" label="编号" sortable>
			</el-table-column>

			<el-table-column label="操作" width="150">
				<template scope="scope">
					<el-button type="info" size="small" @click="handleEdit(scope.$index, scope.row)" round>编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)" round>删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0" round>批量删除</el-button>
      <el-switch v-model="value" style="float:right;">
      </el-switch>
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :page-sizes="[5, 10, 15, 20, 25]"
          :page-size="pageSize"
          :current-page="currentPage"
          :total="total"
          :hide-on-single-page="value"
          layout="total, sizes, prev, pager, next, jumper"
          style="float:right;">
      </el-pagination>

		</el-col>

		<!--新增和编辑界面-->
		<el-dialog :title="myTitle" :visible.sync="editFormVisible" :close-on-click-modal="false">
			<el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
				<el-form-item label="姓名" prop="name">
					<el-input v-model="editForm.name" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="编号" prop="sn">
					<el-input v-model="editForm.sn" auto-complete="off"></el-input>
				</el-form-item>

			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>



    <!--==============================================================内层抽屉===========================================================================-->
    <el-drawer
        title="字典明细:"
        :visible.sync="dictabli"
        direction="rtl"
        size="50%">

      <!--工具条-->
      <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
        <el-form :inline="true" :model="dicFilters">
          <el-form-item>
            <el-input v-model="dicFilters.keyword" placeholder="关键字"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" v-on:click="searchDis" round>查询</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="dicAdd" round>新增</el-button>
          </el-form-item>
        </el-form>
      </el-col>

      <!--字典明细列表展示-->
      <el-table :data="dictionaryDetails" highlight-current-row v-loading="listLoading"
                @selection-change="selsDisChange" style="width: 100%;">
        <el-table-column type="selection" width="55">
        </el-table-column>
        <el-table-column type="index" width="60">
        </el-table-column>
        <el-table-column prop="name" label="名称" sortable>
        </el-table-column>
        <el-table-column prop="dictionaryType.name" label="品牌类型" sortable>
        </el-table-column>
        <el-table-column label="操作" width="150">
          <template scope="scope">
            <el-button type="info" size="small" @click="dicEdit(scope.$index, scope.row)" round>编辑</el-button>
            <el-button type="danger" size="small" @click="dicDel(scope.$index, scope.row)" round>删除</el-button>
          </template>
        </el-table-column>
      </el-table>


      <!--工具条-->
      <el-col :span="24" class="toolbar">
        <el-button type="danger" @click="batchDicRemove" :disabled="this.selsDis.length===0" round>批量删除</el-button>
        <el-switch v-model="valueDis" style="float:right;">
        </el-switch>
        <el-pagination
            @size-change="handleDisSizeChange"
            @current-change="handleDisCurrentChange"
            :page-sizes="[5, 10, 15, 20, 25]"
            :page-size="pageDisSize"
            :current-page="currentDisPage"
            :total="totalDis"
            :hide-on-single-page="valueDis"
            layout="total, sizes, prev, pager, next, jumper"
            style="float:right;">
        </el-pagination>
      </el-col>

    </el-drawer>
    <!--新增和编辑界面-->
    <el-dialog :title="myDicTitle" :visible.sync="editDicFormVisible" :close-on-click-modal="false">
      <el-form :model="editDicForm" label-width="80px" ref="editDicForm">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="editDicForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <!--下拉菜单-->
        <el-form-item label="类型" >
          <el-select v-model="editDicForm.dictionaryType" value-key="id" placeholder="请选择">
            <el-option
                v-for="item in dictionaryTypes"
                :key="item.id"
                :label="item.name"
                :value="item">
              <span style="float: left">{{ item.name }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.id}}</span>
            </el-option>
          </el-select>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="editDicFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="editDicSubmit" :loading="editDisLoading">提交</el-button>
      </div>
    </el-dialog>

  </section>
</template>

<script>

	export default {
		data() {
			return {

        dictabli:false,
        totalDis: 0,
        pageDisSize:5,
        currentDisPage: 1,
        listLoadingDis: false,
        selsDis: [],
        //只有一页时隐藏分页条
        valueDis: false,
        //头
        myDicTitle:"",
        dicFilters: {
          keyword: ''  //关键字查询
        },
        editDicFormVisible: false,

        editDicForm:{
          id: null,
          name: "",
          types_id: "",
          dictionaryType: {
            name
          }
        },



			  myTitle:"",
				filters: {
          keyword: ''  //关键字查询
				},
        dictionaryDetails: [],
				dictionaryTypes: [],
        //只有一页时隐藏分页条
        value: false,

        //分页
				total: 0,
        pageSize:5,
        currentPage: 1,
				listLoading: false,
				sels: [],//当前列表选中的所有行

        types_id:0,

        //数据明细列表所选行
        dissels:[],

				editFormVisible: false,//编辑界面是否显示
				editLoading: false,
        //表单的校验规则
				editFormRules: {
					name: [
					    //设置权限姓名必填
						{ required: true, message: '请输入姓名', trigger: 'blur' }
					]
				},


				//编辑界面数据
				editForm: {
					id: null,
          name: "",
          types_id: "",

				}

			}
		},
		methods: {

      openClick(row){
        this.dicFilters.keyword=""
        this.dictabli=true;
        this.types_id = row.id;
        this.getDictionaryDetails()

      },

      search(){//高级查询一定要定位到第一页
        this.currentPage = 1;
        this.getDictionaryTypes();
      },
			handleCurrentChange(val) {
				this.currentPage = val;
				this.getDictionaryTypes();
			},
      handleSizeChange(sizes){
        this.pageSize = sizes;
        this.getDictionaryTypes();
      },

      searchDis(){//高级查询一定要定位到第一页
        this.currentDisPage = 1;
        this.getDictionaryDetails();
      },
      handleDisCurrentChange(val) {
        this.currentDisPage = val;
        this.getDictionaryDetails();
      },
      handleDisSizeChange(sizes){
        this.pageDisSize = sizes;
        this.getDictionaryDetails();
      },

      //获取字典明细
      getDictionaryDetails() {
        let para = {
          currentPage: this.currentDisPage,
          pageSize: this.pageDisSize,
          keyword: this.dicFilters.keyword,
          id:this.types_id
        };
        this.listLoading = true;//开启列表忙等
        this.$http.post("/dicDetail",para)
          .then(result=>{
            this.listLoading = false;//关闭列表忙等
            result  = result.data;//PageList
            this.dictionaryDetails = result.rows;
            this.totalDis = result.total;
          })
          .catch(result=>{
            console.log(result);
            this.$message({
              message: '系统繁忙，稍后重试！',
              type: 'error'
            });
          })
      },
      //删除
      dicDel: function (index, row) {
        this.$confirm('确认删除该记录吗?', '温馨提示', {
          type: 'warning'
        }).then(() => {//确定
          this.listLoading = true;//开启列表加载忙等
          this.$http.delete("/dicDetail/"+row.id)//http://localhost:8080/dicType/1
              .then(result=>{
                this.listLoading = false;//关闭列表加载忙等
                result = result.data;//AjaxResult
                if(!result.success){
                  this.$message({
                    message: result.message,
                    type: 'error'
                  });
                }
                //数据删除要定位到第一页
                this.currentDisPage = 1;
                this.getDictionaryDetails();
              })
              .catch(result=>{
                console.log(result);
                this.$message({
                  message: '系统繁忙，稍后重试！',
                  type: 'error'
                });
              })
        }).catch(() => {//取消

        });
      },

      selsDisChange: function (sels) {//当前页面，所有选中的行的数据
        this.selsDis = sels;//把当前选中的所有行的数据，绑定给模型层，一会儿批量删除的时候用
      },
      //批量删除
      batchDicRemove: function () {
        //获取id为数组
        var ids = this.selsDis.map(item => item.id);
        this.$confirm('确认删除选中记录吗？', '温馨提示', {
          type: 'warning'
        }).then(() => {//确定删除
          this.$http.patch("/dicDetail",ids)
              .then(result=>{
                result = result.data;//AjaxResult
                if(!result.success){
                  this.$message({
                    message: result.message,
                    type: 'error'
                  });
                }
                //数据删除要定位到第一页
                this.currentDisPage = 1;
                this.getDictionaryDetails();
              })
              .catch(result=>{
                console.log(result);
                this.$message({
                  message: '系统繁忙，稍后重试！',
                  type: 'error'
                })
              })
        }).catch(() => {

        });
      },

      //显示新增界面
      dicAdd: function () {
        this.myTitle = "新增";
        this.editDicFormVisible = true;
        this.editDicForm = {//重置表单
          id: null,
          name: "",
          types_id:this.types_id,
          dictionaryType:null
        };
      },

      //显示编辑界面
      dicEdit: function (index, row) {
        this.myTitle = "编辑";
        this.editDicFormVisible = true;
        //克隆  用于回显
        this.editDicForm = Object.assign({}, row);
      },

      //编辑
      editDicSubmit: function () {
        //this.$refs找到页面上所有的ref请求
        //this.$refs.editForm  找editForm
        this.$refs.editDicForm.validate((valid) => {
          if (valid) {
            this.$confirm('确认提交吗？', '提示', {}).then(() => {
              //开启按钮忙等
              this.editLoading = true;
              //克隆数据
              let para = Object.assign({}, this.editDicForm);

              this.$http.put("/dicDetail",para)
                  .then(result=>{
                    //关闭提交按钮忙等
                    this.editLoading = false;
                    result = result.data;
                    if(!result.success){
                      this.$message({
                        message:result.message,
                        type:"error"
                      })
                    }else {
                      this.$message({
                        message: result.message,
                        type: 'success'
                      });
                      //成功才会关闭模态框
                      this.editDicFormVisible = false;
                    }
                    //定位到第一页
                    this.currentDisPage = 1;
                    //重新查询
                    this.getDictionaryDetails();
                  })
                  .catch(result=>{
                    this.editLoading = false;
                    this.$message({
                      message: '系统繁忙，稍后重试！',
                      type: 'error'
                    });
                  })
            });
          }
        });
      },









/*=======================================================================================================*/
			//获取字典类型
			getDictionaryTypes() {
				let para = {
          currentPage: this.currentPage,
					pageSize: this.pageSize,
          keyword: this.filters.keyword
				};
				this.listLoading = true;//开启列表忙等
        this.$http.post("/dicType",para)
        .then(result=>{
             this.listLoading = false;//关闭列表忙等
            result  = result.data;//PageList
            this.dictionaryTypes = result.rows;
            this.total = result.total;
        })
        .catch(result=>{
          console.log(result);
          this.$message({
            message: '系统繁忙，稍后重试！',
            type: 'error'
          });
        })
			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '温馨提示', {
					type: 'warning'
				}).then(() => {//确定
					this.listLoading = true;//开启列表加载忙等
					this.$http.delete("/dicType/"+row.id)//http://localhost:8080/dicType/1
          .then(result=>{
            this.listLoading = false;//关闭列表加载忙等
            result = result.data;//AjaxResult
            if(!result.success){
              this.$message({
                message: result.message,
                type: 'error'
              });
            }
            //数据删除要定位到第一页
            this.currentPage = 1;
            this.getDictionaryTypes();
          })
          .catch(result=>{
            console.log(result);
            this.$message({
              message: '系统繁忙，稍后重试！',
              type: 'error'
            });
          })
				}).catch(() => {//取消

				});
			},
			//显示编辑界面
			handleEdit: function (index, row) {
        this.getDeptTree()
        this.myTitle = "编辑";
				this.editFormVisible = true;
				//克隆  用于回显
				this.editForm = Object.assign({}, row);
        let dirPath = this.editForm.dirPath;
        if(dirPath){
          //以/分割
          let arry = dirPath.split("/");//  ["","2","5","8"]
          //声明空的数组
          let parent = [];//[2,5]
          //遍历数组arry,截取需要的片段
          for(let i=1;i<arry.length-1;i++){
            //parseInt(arry[i])从字符串数组转为int类型数组,将获取到的数据插入空数组中
            parent.push(parseInt(arry[i]));
          }
          //将数组赋值给模型层
          this.editForm.parent = parent;
        }

			},
			//显示新增界面
			handleAdd: function () {
        this.getDeptTree()
        this.myTitle = "新增";
        this.editFormVisible = true;
				this.editForm = {//重置表单
          id: null,
          name: "",
          sn: "",
        };
			},
			//编辑
			editSubmit: function () {
        //this.$refs找到页面上所有的ref请求
        //this.$refs.editForm  找editForm
				this.$refs.editForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
						  //开启按钮忙等
							this.editLoading = true;
							//克隆数据
							let para = Object.assign({}, this.editForm);
              if(para.parent){//[2,7]   --   {"id":7}
                para.parent = {"id":para.parent[para.parent.length -1]}
              }
							this.$http.put("/dicType",para)
              .then(result=>{
                //关闭提交按钮忙等
                this.editLoading = false;
                result = result.data;
                if(!result.success){
                  this.$message({
                    message:result.message,
                    type:"error"
                  })
                }else {
                  this.$message({
                    message: result.message,
                    type: 'success'
                  });
                  //成功才会关闭模态框
                  this.editFormVisible = false;
                }
                //定位到第一页
                this.currentPage = 1;
                //重新查询
                this.getDictionaryTypes();
              })
              .catch(result=>{
                this.editLoading = false;
                this.$message({
                  message: '系统繁忙，稍后重试！',
                  type: 'error'
                });
              })
						});
					}
				});
			},
			selsChange: function (sels) {//当前页面，所有选中的行的数据
				this.sels = sels;//把当前选中的所有行的数据，绑定给模型层，一会儿批量删除的时候用
			},
			dicChange: function (sels) {//当前页面，所有选中的行的数据
				this.dissels = sels;//把当前选中的所有行的数据，绑定给模型层，一会儿批量删除的时候用
			},
			//批量删除
			batchRemove: function () {
        //拿到当前选中的所有行的id，传给后端批量删除
         // [1,2,3]  .toString() --> "1,2,3"
				var ids = this.sels.map(item => item.id);//删除了toString
				this.$confirm('确认删除选中记录吗？', '温馨提示', {
					type: 'warning'
				}).then(() => {//确定删除
				  this.$http.patch("/dicType",ids)
              .then(result=>{
                result = result.data;//AjaxResult
                if(!result.success){
                  this.$message({
                    message: result.message,
                    type: 'error'
                  });
                }
                //数据删除要定位到第一页
                this.currentPage = 1;
                this.getDictionaryTypes();
              })
              .catch(result=>{
                console.log(result);
                this.$message({
                  message: '系统繁忙，稍后重试！',
                  type: 'error'
                })
              })
				}).catch(() => {

				});
			},
      getEmployees(){
        this.$http.get("/employee")
            .then(result=>{
              this.employees = result.data;
            })
      },
      getDeptTree(){
        this.$http.get("/dicType/tree")
            .then(result=>{
              this.deptTree = result.data;
            })
      }
		},


		mounted() {
			this.getDictionaryTypes();
      this.getEmployees();
		}
	}

</script>

<style scoped>

</style>